<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '管理员添加' : '管理员修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      {{ user }}
      <el-form :model="user">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="user.roleid">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :value="item.id"
              :label="item.rolename"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>

        <!-- <el-form :model="user"> -->
        <el-form-item label="密码" label-width="120px">
          <el-input
            show-password
            v-model="user.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- </el-form> -->

      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="handleNodeClick"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {

  reqrolelist,
requseradd,
requserinfo,
requseredit,
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils";
export default {
  props: ["info"],
  methods: {
    handleNodeClick() {
      requseradd(this.user).then(res => {
        // console.log('添加',res);
        successAlert(res.data.msg);
        this.$emit('init');
        this.empty();
        this.info.isshow = false;
      })
    },
    getOne(id) {
      requserinfo({ uid:id }).then((res) => {
        this.user = res.data.list;
        this.user.password = '';
        console.log(res);
      });
    },
    empty() {
      this.user = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    close() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    update() {
      // this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      requseredit(this.user).then((res) => {
        // console.log(res);
        successAlert(res.data.msg);
        this.empty();
        this.info.isshow = false;
        this.$emit("init");
      });
    },
  },
  mounted() {
    // reqmenulist({ istree: true }).then((res) => {
    //   this.treedata = res.data.list ? res.data.list : [];
    // });
    reqrolelist().then((res) => {
      if (res.data.code === 200) {
        // console.log(res);
        this.roleList = res.data.list ? res.data.list : [];
      }
    });
  },

  data() {
    return {
      user: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      roleList: [],
    };
  },
};
</script>

<style>
</style>